﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.物料管理
{
    /// <summary>
    /// 物料分组
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMaterialGroupController : ControllerBase
    {
        public TMaterialGroupController(ITMaterialGroupRepository tMaterialGroupRepository)
        {
            TMaterialGroupRepository = tMaterialGroupRepository;
        }

        public ITMaterialGroupRepository TMaterialGroupRepository { get; }

        /// <summary>
        /// 添加物料分组
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTMaterialGroup(TMaterialGroup m)
        {
            bool IsOK = TMaterialGroupRepository.AddTMaterialGroup(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 删除物料分组
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTMaterialGroup(int mid)
        {
            if(mid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = TMaterialGroupRepository.DelTMaterialGroup(mid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 修改物料分组
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdTMaterialGroup(TMaterialGroup m)
        {
            if(m.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = TMaterialGroupRepository.UpdTMaterialGroup(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 根据Id获取对象信息
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTMaterialGroupById(int mid)
        {
            if(mid<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var rule = TMaterialGroupRepository.GetTMaterialGroupById(mid);
            if (rule != null)
            {
                return Ok(BaseResult.Result(rule, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！Id可能有误或已被删除！", "500"));
        }

        /// <summary>
        /// 显示物料分组列表
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTMaterialGroupList(string? mname, int pageIndex=1, int pageSize=10)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = TMaterialGroupRepository.GetTMaterialGroupList(mname, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
